$(document).ready(function() {
	$("input[type='file']").change(function() {
		var path = $(this).val();
		var filename = path.substring(path.lastIndexOf("\\")+1, path.length);
		$("#selectedfilename").html(filename);
	});
	$("form").submit(function(){
		$("input[name='competitionname']+span,input[name='picturedesc']+span,input[name='picturename']+span").css("display","none");
		var formdata = new FormData();
        formdata.append("competitionName", $("input[name='competitionname']").val());
        formdata.append("pictureDesc", $("input[name='picturedesc']").val());
        formdata.append("pictureName", $("input[type='file']")[0].files[0]);
        $.ajax({
            url:"AddPicture.do",
            type:"POST",
            data:formdata,
            cache: false,
            processData: false,
            contentType: false
        }.done(function(result){
                result = eval('(' + result + ')');
                if(result != undefined && result != null){
                	if(result.status == 'ok'){
                		alert("保存图片成功");
                		window.location.href="ListPicture.do";
                	}else if(result.status == 'error'){
                		alert(result.msg);
                	}
                }
                if(result.competitionname != undefined && result.competitionname != null){
                	$("input[name='competitionname']+span").html("<i class=\"icon iconfont\">&#xe624;</i>"+result.competitionname);
                	$("input[name='competitionname']+span").css("display","inline-block");
                }
                if(result.picturedesc != undefined && result.picturedesc != null){
                	$("input[name='picturedesc']+span").html("<i class=\"icon iconfont\">&#xe624;</i>"+result.picturedesc);
                	$("input[name='picturedesc']+span").css("display","inline-block");
                }
                if(result.picturename != undefined && result.picturename != null){
                	$("input[name='picturename']+span").html("<i class=\"icon iconfont\">&#xe624;</i>"+result.picturename);
                	$("input[name='picturename']+span").css("display","inline-block");
                }
            }
        ).fail(function(){
        	alert("fail");
        }));
        return false;
	})
})